import 'dart:convert';

import 'package:flutter/material.dart';
import '../../global/Global.dart';
import '../../global/GlobalUrl.dart';
import '../../pages/person/Login.dart';
import '../../pages/view/HeaderView.dart';
import '../../services/ScreenAdapter.dart';
import '../../services/TLSizeFit.dart';
import '../../util/HttpUtil.dart';
import '../../util/LogUtil.dart';
import '../../util/Storage.dart';
import '../../util/ToastUtils.dart';

import '../../YColors.dart';

class SettingPage extends StatefulWidget {
  SettingPage({Key key}) : super(key: key);

  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    TLSizeFit.initialize();
    ScreenAdapter.init(context);

    return Scaffold(
      appBar: HeaderView(
        title: "设置",
        isShowReturnArrow: true,
        leading: IconButton(
          icon: Image.asset(
            "images/return_back.png",
            width: 20,
            height: 20,
          ),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: Container(
        width: TLSizeFit.screenWidth,
        color: YColors.t,
        padding: EdgeInsets.fromLTRB(
            TLSizeFit.setPx(20), TLSizeFit.setPx(20), TLSizeFit.setPx(20), 0),
        child: setBodyView(),
      ),
    );
  }

  setBodyView() {
    return Column(
      children: <Widget>[
        Container(
            alignment: Alignment.center,
            child: InkWell(
              child: Text(
                "退出登录",
              ),
              onTap: () {
                logoutHttp();
              },
            ))
      ],
    );
  }

  /**
   * 退出登录
   */
  logoutHttp() async {
    String url = GlobalUrl.LOGOUTURL;
    var map = {"": ""};
    String access_token = await Storage.getToken();
    LogUtil.init(title: "来自LogUtil", isDebug: false, limitLength: 800);
    var mapHeaders = {
      Global.AUTHORIZATION: access_token,
      Global.CONTENT_TYPE: Global.APPLICATION_JSON
    };

    HttpUtil.delete(url,
        data: map,
        headers: mapHeaders,
        success: (data) {
          print("退出了登录了");
//          接口的数据：{code: 200, success: true, data: null, msg: 注销成功}
          var success = data["success"];
//      LogUtil.v("success:$success");
          var message = data["msg"];
          var body = data["data"];
          var code = data["code"];
          if(success==true){
            Storage.clear(); // 清除数据
            //跳转并关闭当前页面
            Navigator.pushAndRemoveUntil(
              context,
              new MaterialPageRoute(builder: (context) => new LoginPage()),
                  (route) => route == null,
            );

          }
          ToastUtils.toast(message);

        },
        error: (errorMsg) {

        });
  }
}
